package collectionFrame;

import java.security.Policy;
import java.util.*;

public class Demo5_CollectionFirstExample {

    public static void chooseTeam(){

        //初始化所有的球队
        List<String> list_team_all = Arrays.asList("科特迪瓦","阿根廷","澳大利亚","塞尔维亚","荷兰","尼日利亚","日本","美国","中国",
                "新西兰","巴西","比利时","韩国","喀麦隆","洪都拉斯","意大利");

        //此列表存放的是4个队伍，每一个队伍也以集合形式表示
        List<List<String>> list_teams = new ArrayList<>();

        //对原始的16支队伍打乱顺序
        Collections.shuffle(list_team_all);

        //循环四次，找出四个小组
        for (int i = 0; i < list_team_all.size(); i+=4) {
            //打乱顺序后的队伍，每四个球队组成一个小组
            list_teams.add(list_team_all.subList(i,i+4));
        }

        System.out.println(list_teams);


    }


    public static void lottery(){

        //定义一个随机数对象，此对象尽量放在类中或方法中，不要放在循环中，避免创建多个对象，影响性能开销
        Random random = new Random();

        //初始化用于存放结果的集合
        List<List<Integer>> list_numWin = new ArrayList<>();

        //初始化一个存储 0-22 的数字的集合
        List<Integer> list_nums = new ArrayList<>();
        //向集合中添加数字
        for (int i = 0; i < 23; i++) {
            list_nums.add(i);
        }

        //对23个数字打乱顺序
        Collections.shuffle(list_nums);

        //抽取五组中奖号码
        for (int i = 0; i < 5; i++) {
            int startIndex = random.nextInt(list_nums.size()-5);
            int endIndex = startIndex + 5;
            list_numWin.add(list_nums.subList(startIndex,endIndex));
        }

        System.out.println(list_numWin);

    }

    public static void sortPoker(){

        //存放52张牌的集合
        List<Poker> list_poker = new ArrayList<>();
        //第一层遍历所有的花色
        for (int i = 0; i < Poker.colors.length; i++) {
            //第二层遍历所有的数值
            for (int j = 0; j < Poker.values.length; j++) {
                list_poker.add(new Poker(i,j));
            }
        }

        System.out.println(list_poker);

        //打乱原来的扑克牌顺序
        Collections.shuffle(list_poker);
        System.out.println(list_poker);

        //按照默认规则排序
        Collections.sort(list_poker);
        System.out.println(list_poker);

        //再次打乱顺序
        Collections.shuffle(list_poker);
        System.out.println(list_poker);

        //按照自定义规则排序
        Collections.sort(list_poker,new PokerComparator());
        System.out.println(list_poker);

    }

    public static void main(String[] args) {
//        Demo5_CollectionFirstExample.chooseTeam();
//        Demo5_CollectionFirstExample.lottery();
        Demo5_CollectionFirstExample.sortPoker();
    }

}

/**
 * 自定义一个扑克类，属性有花色和数值，有默认的排序规则(先按花色排序，按数值排序)
 */
class Poker implements Comparable<Poker>{

    //初始化扑克的所有花色和所有的数值，必须按照顺序定义
    public static final String[] colors = {"黑桃","红桃","草花","方块"};
    public static final String[] values = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};

    //扑克的花色(4种花色分别用0,1,2,3表示，方便后续定义比较规则)
    private int color;
    //扑克的数值(13种数值分别用0-12表示，方便后续定义比较规则)
    private int value;

    public Poker(int color, int value) {
        this.color = color;
        this.value = value;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return Poker.colors[color] + Poker.values[value];
    }

    @Override
    public int compareTo(Poker o) {

        int thisColor = this.color;
        int thisValue = this.value;
        int thatColor = o.getColor();
        int thatValue = o.getValue();

        //如果花色相等，比较数值
        if(thisColor == thatColor){
            return thisValue - thatValue;
        }else{
            //如果花色不同，直接比较花色
            return thisColor - thatColor;
        }

    }
}

/**
 * 自定义Poker类的排序规则
 */
class PokerComparator implements Comparator<Poker>{

    @Override
    public int compare(Poker o1, Poker o2) {
        int thisColor = o1.getColor();
        int thisValue = o1.getValue();
        int thatColor = o2.getColor();
        int thatValue = o2.getValue();

        //如果数值相等，比较花色
        if(thisValue == thatValue){
            return thisColor - thatColor;
        }else{
            //如果数值不同，直接比较数值
            return thisValue - thatValue;
        }
    }
}